package com.sdry.product.prod.service;
import java.sql.Timestamp;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.sdry.basic.entity.Material;
import com.sdry.common.BaseAPIMessageTypeEnum;
import com.sdry.exception.BizException;
import com.sdry.product.prod.entity.LacquerInto;
import com.sdry.product.prod.entity.ResultAppList;
import com.sdry.product.prod.mapper.LacquerIntoMapper;
import com.sdry.product.prod.mapper.ProductTwoPaintMapper;
import com.sdry.product.prod.entity.ProductTwoPaint;
import com.sdry.untils.CustomUtil;
import com.sdry.untils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ProductTwoPaint)表服务实现类
 *
 * @author makejava
 * @since 2020-03-05 15:13:02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ProductTwoPaintServiceImpl implements ProductTwoPaintService {
    @Resource
    private ProductTwoPaintMapper productTwoPaintDao;

    @Resource
    private LacquerIntoMapper lacquerIntoMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param paId 主键
     * @return 实例对象
     */
    @Override
    public ProductTwoPaint queryById(Long paId) {
        return this.productTwoPaintDao.queryById(paId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<ProductTwoPaint> queryAllByLimit(int offset, int limit) {
        return this.productTwoPaintDao.queryAllByLimit(offset, limit);
    }


    /**
     * 修改数据
     *
     * @param productTwoPaint 实例对象
     * @return 实例对象
     */
    @Override
    public ProductTwoPaint update(ProductTwoPaint productTwoPaint) {
        this.productTwoPaintDao.update(productTwoPaint);
        return this.queryById(productTwoPaint.getPaId());
    }

    /**
     * 通过主键删除数据
     *
     * @param paId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long paId) {
        return this.productTwoPaintDao.deleteById(paId) > 0;
    }

    /**
     * 分页查询
     * @param productTwoPaint 实例对象
     * @return 实例对象
     */
    @Override
    public List<ProductTwoPaint> queryProductTwoPaintPage(ProductTwoPaint productTwoPaint) {
        if (StringUtils.isEmpty(productTwoPaint.getPsNumber())){
            productTwoPaint.setPsNumber(null);
        }
        List<ProductTwoPaint> productTwoPaintIPage = productTwoPaintDao.queryProductTwoPaintPage(productTwoPaint);
        return productTwoPaintIPage;
    }

    /**
     * 总数
     * @param productTwoPaint 实例对象
     * @return 总数
     */
    @Override
    public Long queryProductTwoPaintTotal(ProductTwoPaint productTwoPaint) {
        if (StringUtils.isEmpty(productTwoPaint.getPsNumber())){
            productTwoPaint.setPsNumber(null);
        }
        return productTwoPaintDao.queryProductTwoPaintTotal(productTwoPaint);
    }

    @Override
    public void addProductTwoPaintApp(String json, String userId) {
        Gson gson = new Gson();
        List<ResultAppList<Material>> resultAppList = gson.fromJson(json, new TypeToken<List<ResultAppList<Material>>>() {}.getType());
        for (ResultAppList<Material> materialResultAppList : resultAppList) {
            List<Material> materialResultAppListList = materialResultAppList.getList();
            for (Material material : materialResultAppListList) {
                ProductTwoPaint productTwoPaint = new ProductTwoPaint();
                //物料ID
                productTwoPaint.setMaterielId(material.getMaId());
                //产品数量
                productTwoPaint.setQuantity(material.getQuantity());
                //喷涂要求
                productTwoPaint.setSprayingRequirements(material.getMaSprayingRequirements());
                //质检状态
                productTwoPaint.setStatus("");
                if (materialResultAppList.getNewPsId() == null) {
                    throw new BizException(BaseAPIMessageTypeEnum.A098.getName());
                }
                //喷涂工位ID
                productTwoPaint.setPsId(Long.parseLong(materialResultAppList.getNewPsId()));
                //喷涂工位编号
                productTwoPaint.setPsNumber(materialResultAppList.getNewPsnumber());
                //原来的喷涂工位ID
                productTwoPaint.setOldPsId(Long.parseLong(materialResultAppList.getPsId()));
                //原来的喷涂工位编号
                productTwoPaint.setOldPsNumber(materialResultAppList.getPsnumber());
                //质检时间
                productTwoPaint.setExamineDate("");
                //质检人员
                productTwoPaint.setExamineBy(0L);
                //备注
                productTwoPaint.setRemark("0");
                productTwoPaint.setCreateBy(userId);
                productTwoPaintDao.insert(productTwoPaint);
                //更改喷涂线状态
                LacquerInto lacquerInto = new LacquerInto();
                lacquerInto.setStatus("2");
                QueryWrapper<LacquerInto> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("status",0L)
                        .eq("materiel_id",material.getMaId())
                        .eq("station_id",materialResultAppList.getPsId());
                lacquerIntoMapper.update(lacquerInto,queryWrapper);
            }
        }
    }

    @Override
    public List<ResultAppList<Material>> queryProductTwoPaintMateriel(String psnumber) {
        List<Material> materials = productTwoPaintDao.queryProductTwoPaintMateriel(psnumber);
        return CustomUtil.QueryResultChange(materials);
    }

    @Override
    public void updateProductTwoPaintApp(String json, String userId) {
        Gson gson = new Gson();
        List<ResultAppList<Material>> resultAppList = gson.fromJson(json, new TypeToken<List<ResultAppList<Material>>>() {}.getType());
        for (ResultAppList<Material> materialResultAppList : resultAppList) {
            List<Material> materialResultAppListList = materialResultAppList.getList();
            for (Material material : materialResultAppListList) {
                ProductTwoPaint productTwoPaint = new ProductTwoPaint();
                productTwoPaint.setPaId(Long.parseLong(material.getRemark04()));
                productTwoPaint.setQuantity(material.getQuantity());
                productTwoPaint.setUpdateBy(userId);
                productTwoPaint.setUpdateDate(new Timestamp(System.currentTimeMillis()));
                productTwoPaint.setRemark("1");
                productTwoPaintDao.updateById(productTwoPaint);
            }
        }
    }
}